package com.example.Sao.MyFragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.MyFragment
 * @ClassName: LazyloadFragment
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/2/21 17:04
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/2/21 17:04
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public abstract class LazyloadFragment extends Fragment {
    // 布局成功已经加载的标志位
    private boolean isViewLoaded = false;
    // 数据已经加载的标志位
    private boolean isDataLoaded = false;
    // contentView
    private View contentView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initConfiguration(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.contentView = inflater.inflate(getContentLayout(), container, false);
        this.isViewLoaded = true;
//        final LocalDialogFragment dialogFragment = new LocalDialogFragment();
//        dialogFragment.show(getFragmentManager(),"");
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                dialogFragment.dismiss();
//            }
//        },300);
        return contentView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getUserVisibleHint() && isViewLoaded){
            if (!isDataLoaded){
                initView(getContentView());
                this.isDataLoaded = loadData();
            }
        }
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint() && isViewLoaded ){
            if (!isDataLoaded){
                initView(getContentView());
                this.isDataLoaded = loadData();
            }
        }
    }

    @Override
    public void onDestroyView() {
        // 若View被销毁了，则需要重新创建View，此时应该把标志位复原
        this.isViewLoaded = false;
        isDataLoaded = false;
        super.onDestroyView();
    }

    /**
     * 用于子类得到contentView
     * @return contentView
     */
    public View getContentView(){
        return this.contentView;
    }

    /**
     * 该方法在Fragment的OnCreate方法被执行时调用
     * 主要用于初始化其他模块传递到fragment中的数据
     * 该方法可以选择性地去实现
     * @param bundle 数据包
     */
    protected void initConfiguration(Bundle bundle){}

    /**
     * 在加载布局前调用，用于获取布局文件资源
     * @return 返回一个布局文件的layout资源
     */
    protected abstract int getContentLayout();

    /**初始化布局
     * @param contentView contentView
     */
    protected void initView(View contentView){}

    /**加载数据
     * @return 返回Boolean类型的值，若为true：说明已经执行了加载数据的操作；若为false：说明未执行加载数据的操作
     */
    protected abstract boolean loadData();
}

